<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        // 声明计算器对象
        let calculator = {
            // 加
            sum() {
                return this.a + this.b;
            },
            // 减
            sub() {
                return this.a - this.b;
            },
            // 乘
            mul() {
                return this.a * this.b;
            },
            // 除
            div() {
                if (this.b === 0) {
                    alert("被除数不能为0");
                    return null;
                } else {
                    return this.a / this.b;
                }
            },
            // 读取待计算数
            read() {
                // 输入
                while (this.a == null || this.b == null) {
                    this.a = this.a == null ? prompt('a?', 0) : this.a;
                    this.b = this.b == null ? prompt('b?', 0) : this.b;
                }
                // 转换为int类型
                this.a = Number.parseInt(this.a);
                this.b = Number.parseInt(this.b);
            }
        };

        // 读取待计算数 a，b
        calculator.read();

        // 读取运算符号
        let operationSymbol = prompt("运算符号为", "+");

        // 计算
        switch (operationSymbol) {
            case "+":
                alert("相加结果为" + calculator.sum());
                break;
            case "-":
                alert("相减结果" + calculator.sub());
                break;
            case "*":
                alert("相乘结果" + calculator.mul());
                break;
            case "/": {
                alert("相除结果" + calculator.div());
            }
                break;
            default:
                alert("请输入正确的运算符");
                break;
        }
    </script>
</head>
<body></body>
</html>